package com.javaketang.dky.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaketang.dky.entity.EasybuyNews;
import com.javaketang.dky.mapper.EasybuyNewsMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dky
 * @since 2021-07-06
 */
@Controller
@RequestMapping("news")
public class EasybuyNewsController {

    //新闻类
    @Resource
    private EasybuyNewsMapper newsMapper;

    /**
     * 咨询列表
     * @param action
     * @param current
     * @param session
     * @return
     */
    @RequestMapping("/consult")
    public String consult(String action, Integer current, HttpSession session, Model model) {

        //判断不为空时
        if (action != null && action != "") {
            //判断是咨询列表
            if (action.equals("consult")) {

                //分页
                Page<EasybuyNews> page = new Page<>(current == null ? 1 : current, 10);

                IPage<EasybuyNews> easybuyNewsIPage = newsMapper.selectPage(page, null);

                //获取总页数
                int sumDateNumber = (int) (page.getTotal() % 10 == 0 ? page.getTotal() / 10 : page.getTotal() / 10 + 1);

                System.out.println("数据总页数--》》" + sumDateNumber);

                //赋值到集合中
                List<EasybuyNews> list = easybuyNewsIPage.getRecords();

                list.forEach(System.out::println);

                model.addAttribute("lists",list);
                model.addAttribute("sumPage",sumDateNumber);
                model.addAttribute("current",current == null ? 1 : current);

            }
        }

        session.setAttribute("action", "consult");

        return "Member_User";
    }

    /**
     * 查询单条新闻
     * @param id  新闻id
     * @param session
     * @return
     */
    @RequestMapping("/newsDeatil")
    public String newsDeatil(Integer id,HttpSession session,Model model){

        //查询单条新闻
        EasybuyNews news = newsMapper.selectById(id);

        //赋值model
        model.addAttribute("Einzelband",news);

        session.setAttribute("action","newsDeatil");

        return "Member_User";
    }

}

